﻿using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace FangChanCrm.Services.Realization
{
    public class t_QuanXianPeiZhiBiaoService : It_QuanXianPeiZhiBiaoService
    {
        public readonly MyContext _Context;
        public t_QuanXianPeiZhiBiaoService(MyContext context)
        {
            _Context = context;
        }
        public t_QuanXianPeiZhiBiao Add(t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao)
        {
            _Context.t_QuanXianPeiZhiBiao.Add(t_QuanXianPeiZhiBiao);
            _Context.SaveChanges();
            return _Context.t_QuanXianPeiZhiBiao.FirstOrDefault(x => x.BeiZhu1 == t_QuanXianPeiZhiBiao.BeiZhu1 && x.BeiZhu2 == t_QuanXianPeiZhiBiao.BeiZhu2 && x.BeiZhu3 == t_QuanXianPeiZhiBiao.BeiZhu3 && x.BeiZhu4 == t_QuanXianPeiZhiBiao.BeiZhu4 && x.BeiZhu5 == t_QuanXianPeiZhiBiao.BeiZhu5 && x.PeiZhiMingCheng == t_QuanXianPeiZhiBiao.PeiZhiMingCheng && x.QuanXianId==t_QuanXianPeiZhiBiao.QuanXianId);

        }

        public bool Delete(int id)
        {
            _Context.t_QuanXianPeiZhiBiao.Remove(_Context.t_QuanXianPeiZhiBiao.Where(x => x.Id == id).ToList()[0]);
            return _Context.SaveChanges() > 0;
        }

        public t_QuanXianPeiZhiBiao GetById(int id)
        {
            return _Context.t_QuanXianPeiZhiBiao.FirstOrDefault(x => x.Id == id);
        }

        public List<t_QuanXianPeiZhiBiao> GetList()
        {
            return _Context.t_QuanXianPeiZhiBiao.ToList();
        }
        public List<t_QuanXianPeiZhiBiao> GetList1()
        {
            return _Context.t_QuanXianPeiZhiBiao.Where("BeiZhu1==\"启用\"").ToList();
        }
        
        public List<t_QuanXianPeiZhiBiao> listshuju()
        {
            t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao = new t_QuanXianPeiZhiBiao();
            return _Context.t_QuanXianPeiZhiBiao.Where(ReturnWhere(t_QuanXianPeiZhiBiao)).ToList();
        }
        public string YiFenPeiFenYe(t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao, int start, int limit)
        {
            return Helper.ListToJsonAddFenYe(_Context.t_QuanXianPeiZhiBiao.Where(ReturnWhere(t_QuanXianPeiZhiBiao)).ToList(), start, limit);
        }
        public string ReturnWhere(t_QuanXianPeiZhiBiao t_QuanXianPeiZhiBiao)
        {
            string returnstr = "1=1";
            if (t_QuanXianPeiZhiBiao.BeiZhu1 != null)
            {
                returnstr = returnstr + " and BeiZhu1.Contains(\"" + t_QuanXianPeiZhiBiao.BeiZhu1 + "\")";
            }
            if (t_QuanXianPeiZhiBiao.BeiZhu2 != null)
            {
                returnstr = returnstr + " and BeiZhu2.Contains(\"" + t_QuanXianPeiZhiBiao.BeiZhu2 + "\")";
            }
            if (t_QuanXianPeiZhiBiao.BeiZhu3 != null)
            {
                returnstr = returnstr + " and BeiZhu3.Contains(\"" + t_QuanXianPeiZhiBiao.BeiZhu3 + "\")";
            }
            if (t_QuanXianPeiZhiBiao.BeiZhu4 != null)
            {
                returnstr = returnstr + " and BeiZhu4.Contains(\"" + t_QuanXianPeiZhiBiao.BeiZhu4 + "\")";
            }
            if (t_QuanXianPeiZhiBiao.BeiZhu5 != null)
            {
                returnstr = returnstr + " and BeiZhu5.Contains(\"" + t_QuanXianPeiZhiBiao.BeiZhu5 + "\")";
            }

            if (t_QuanXianPeiZhiBiao.PeiZhiMingCheng != null)
            {
                returnstr = returnstr + " and PeiZhiMingCheng.Contains(\"" + t_QuanXianPeiZhiBiao.PeiZhiMingCheng + "\")";
            }
            if (t_QuanXianPeiZhiBiao.QuanXianId != null)
            {
                returnstr = returnstr + " and QuanXianId.Contains(\"" + t_QuanXianPeiZhiBiao.QuanXianId + "\")";
            }
            return returnstr;
        }
    }
}
